<?php

namespace app\admin\controller\information;

use app\admin\model\information\Led as InformationLed;
use app\admin\model\information\Ledview;
use app\common\controller\Backend;
use Exception;
use tests\thinkphp\library\think\dbTest;
use think\Db;

/**
 * LED灯带管理
 *
 * @icon fa fa-circle-o
 * 
 *      try{
            Led::lightOn([39,40],1);
            $this->success();
        }catch(Exception $e){
           trace(['亮灯失败原因',$e->getMessage()],'error');
           $this->error($e->getMessage());
        }
 */
class Led extends Backend
{
    protected $model = null;
    protected $modelView = null;
    protected $noNeedLogin = [];
    
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new InformationLed();
        $this->modelView = new Ledview();
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
    }
    
    public function index()
    {
        //$this->searchFields='shelves.name,admin.name';

        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }

        [$where, $sort, $order, $offset, $limit] = $this->buildparams(['shelves_name','nickname','color','position']);
        $list = $this->modelView
        ->where($where)
        ->order($sort, $order)
        ->paginate($limit);
        
        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }

    /**
     * 删除
     *
     * @param $ids
     * @return void
     * @throws DbException
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     */
    public function del($ids = null)
    {
        if (false === $this->request->isPost()) {
            $this->error(__("Invalid parameters"));
        }
        $ids = $ids ?: $this->request->post("ids");
        if (empty($ids)) {
            $this->error(__('Parameter %s can not be empty', 'ids'));
        }
        $pk = $this->model->getPk();
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            $this->model->where($this->dataLimitField, 'in', $adminIds);
        }
        $list = $this->model->where($pk, 'in', $ids)
        ->field("id,position,'off' output")
        ->select();
        
        $count = 0;
        Db::startTrans();
        try {
            $post_data = collection($list)->toArray();
            foreach ($list as $item) {
                $count += $item->delete();
            }
            
            InformationLed::postLedApi($post_data);
            unset($post_data);

            Db::commit();
        } catch (PDOException|Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if ($count) {
            $this->success();
        }
        $this->error(__('No rows were deleted'));
    }
}
